 /*
      Avgle X JSBox

      你口袋里的观影利器。

      庞大的在线片源库：

      骑兵、步兵、日韩、欧美，近9万部影片日日更新。

      脚本特点：

      1.无广告困扰，想看就看；

      2.基本还原官网功能，方便省心;

      3.支持收藏，方便巩固温习。

      作者联系：https://t.me/nicked

      Tips: 查看使用说明，请运行程序点击右上角菜单。

     */

 version = 4.3
 $addin.current.version = version
 times = $cache.get("times") || 4
 const avIcon = ""

 const nickIcon = ""

 const avgleIcon = ""

 const wechat = ""

 const filters = {
   "Time": {
     "全部影片": "a",
     "今日新增": "t",
     "本周新增": "w",
     "本月新增": "m"
   },
   "View": {
     "最新添加": "mr",
     "最多观看": "mv",
     "最多评论": "md",
     "最多喜欢": "tf"
   }
 }
 const filterName = {
   "a": "共计",
   "t": "今日新增",
   "w": "本周新增",
   "m": "本月新增",
   "mr": "Most Recent",
   "mv": "Most Viewed",
   "md": "Most Commented",
   "tr": "Top Rated",
   "tf": "Top Favorites"
 }

 const content = ["影片", "合集", "分类", "收藏夹"]

 const filterView = {
   type: "view",
   props: {
     id: "filterView",
     radius: 7,
     bgcolor: $color("white"),
     borderWidth: 1,
     borderColor: $color("tint")
   },
   views: [{
     type: "list",
     props: {
       id: "filtersT",
       //separatorHidden: true,
       rowHeight: 30,
       bgcolor: $color("white"),
       template: [{
         type: "label",
         props: {
           id: "filterLabel",
           bgcolor: $color("white"),
           font: $font(15),
           align: $align.center
         },
         layout: $layout.fill

       }],

       scrollEnabled: false
     },
     events: {
       didSelect(sender, indexPath, data) {
         $device.taptic(0);
         cacheFilters.Time = filters.Time[data.filterLabel.text];
         $("filterView").remove();
         filterExist = false;
         $("videos").contentOffset = $point(0, 0);
         $cache.set("cacheFilters", cacheFilters);
         page = 0;
         $("videos").data = [];
         if (mode == "Cat") {
           getVideoData();
         } else if (mode == "Search") {
           getVideoData();
         } else {
           $("search").text = "";
           mode = "Videos";
           has_next = true
           getVideoData();
         }
       },

     },
     layout: function(make, view) {
       make.top.inset(0)
       make.left.inset(0)
       make.width.equalTo(100)
       make.height.equalTo(145)
     }
   }, {
     type: "list",
     props: {
       id: "filtersV",
       //separatorHidden: true,
       rowHeight: 30,
       bgcolor: $color("white"),
       template: [{
         type: "label",
         props: {
           id: "filterLabel",
           bgcolor: $color("white"),
           font: $font(15),
           align: $align.center
         },
         layout: $layout.fill

       }],

       scrollEnabled: false
     },
     events: {
       didSelect(sender, indexPath, data) {
         $device.taptic(0);
         cacheFilters.View = filters.View[data.filterLabel.text];
         $("filterView").remove();
         filterExist = false;
         $("videos").contentOffset = $point(0, 0);
         $cache.set("cacheFilters", cacheFilters);
         page = 0;
         $("videos").data = [];
         if (mode == "Cat") {
           getVideoData();
         } else if (mode == "Search") {
           getVideoData();
         } else {
           $("search").text = "";
           mode = "Videos";
           has_next = true
           getVideoData();
         }

       },

     },
     layout: function(make, view) {
       make.top.inset(0)
       make.left.equalTo($("filtersT").right)
       make.right.inset(0)
       make.height.equalTo(145)
     }
   }, ],
   layout: function(make, view) {
     make.top.inset(45)
     make.left.inset(15)
     make.width.equalTo(200)
     make.height.equalTo(0)
   }
 }

 const contentView = {
   type: "view",
   props: {
     id: "contentView",
     radius: 7,
     bgcolor: $color("white"),
     borderWidth: 1,
     borderColor: $color("tint")
   },
   views: [{
     type: "list",
     props: {
       id: "contentList",
       //separatorHidden: true,
       rowHeight: 30,
       bgcolor: $color("white"),
       scrollEnabled: false,
       template: [{
         type: "label",
         props: {
           id: "contentLabel",
           bgcolor: $color("white"),
           font: $font(15),
           align: $align.center
         },
         layout: $layout.fill
       }],
     },
     events: {
       didSelect(sender, indexPath, data) {
         $device.taptic(0);
         $("contentView").remove();
         contentExist = false;
         if($("search"))
           $("search").text = "";
         var c = data.contentLabel.text;
         if (c == "影片") {
           
           cacheContent = "影片";
           $cache.set("cacheContent", cacheContent);
           contentMode = "Videos";
           if (CCExist == true) {
                 $("CCView").remove();
                 $("Avgle").add(VFView);
             
           }

           CCExist = false;
           VFExist = true;
           $("next").hidden = false;
           $("pre").hidden = false;
           $("nextB").hidden = false;
           $("preB").hidden = false;
           $("page").hidden = false;
           mode = "Videos";
           $("videos").contentOffset = $point(0, 0);
           $("videos").data = [];
           page = 0;
           has_next = true
           getVideoData();
         } else if (c == "合集") {
           cacheContent = "合集";
           $cache.set("cacheContent", cacheContent);
           cacheFilters.View = "bw";
           $cache.set("cacheFiltes", cacheFilters)
           contentMode = "Collections";
           if (CCExist == true) {
             
               $("CCView").remove();
               $("Avgle").add(CCView);
             
           } else {
             $("VFView").remove()
             $("Avgle").add(CCView);
           }
           CCExist = true;
           VFExist = false;
           $("CCList").contentOffset = $point(0, 0);
           cpage = -1;
           has_next = true
           $("CCList").data = []
           getCollectionData()
         } else if (c == "分类") {
           cacheContent = "分类";
           $cache.set("cacheContent", cacheContent);
           cacheFilters.View = "bw";
           $cache.set("cacheFiltes", cacheFilters)
           contentMode = "Categories"
           if (CCExist == true) {
             
               $("CCView").remove();
               $("Avgle").add(CCView);
             
           } else {
             $("VFView").remove()
             $("Avgle").add(CCView);
           }
           CCExist = true;
           VFExist = false;
           $("CCList").contentOffset = $point(0, 0);
           $("CCList").data = []
           getCategoryData()
         } else if (c == "收藏夹") {
           delPlayer()
           $ui.loading(false)
           cacheContent = "收藏夹";
           $cache.set("cacheContent", cacheContent);
           contentMode = "Favorites"
           if (CCExist == true) {
             
               $("CCView").remove();
               $("Avgle").add(VFView);
             
           }
           CCExist = false;
           VFExist = true;
           $("next").hidden = true;
           $("pre").hidden = true;
           $("nextB").hidden = true;
           $("preB").hidden = true;
           $("page").hidden = true;
           $("search").text = "";
           $device.taptic(0);
           //sender.super.remove();
           contentExist = false;
           if (LocalFavList.length == 0) {
             cacheContent = "影片"
             $cache.set("cacheContent", cacheContent)
             $ui.alert("请先收藏!")
             contentMode = "Videos";
             return
           }
           $("videos").contentOffset = $point(0, 0);
           $("search").placeholder = "共计 " + formatNum(LocalFavList.length) + " 部收藏"
           $("searchResult").text = "";
           $("videos").data = [];
           LocalData.favorite.map(function(i) {
             $("videos").data = $("videos").data.concat({
               interface: {
                 src: i.image
               },
               title: {
                 text: i.title
               },
               time: {
                 title: i.time,
                 info: i.image
               },
               duration: {
                 text: i.duration
               },
               like: {
                 text: i.like,
                 alpha: 0.7

               },
               hd: {
                 hidden: i.hd
               },
               favorite: {
                 title: "😍",
                 alpha: 1,
                 info: i
               },
               share: {
                 info: { vid: i.vid, name: i.title,vidUrl:i.video_url}
               }
             });
           });

         }
       }
     },
     layout: function(make, view) {
       make.top.inset(0)
       make.left.right.inset(0)
       make.width.equalTo(100)
       make.height.equalTo(120)
     }
   }, {
     type: "button",
     props: {
       id: "pay",
       title: "赞赏|提示",
       titleColor: $color("white"),
       bgcolor: $color("#e0b741"),
       font: $font("bold", 15)

     },
     layout: function(make, view) {
       make.top.inset(127)
       make.height.equalTo(25)
       make.left.right.inset(10)
     },
     events: {
       tapped(sender) {
         $device.taptic(0);
         $("contentView").remove();
         contentExist = false;
         $ui.menu({
           items: ["领门店红包", "微信", "使用提示"],
           handler: function(title, idx) {
             switch (idx) {
               case 0:
                 $app.openURL("https://qr.alipay.com/c1x08751v4qb7flvu6dvrf7")
                 break;
               case 1:
                 wechatPay()
                 break;
               case 2:
                 tutorial();
                 break;
             }
           }
         })
       }
     }

   }],
   layout: function(make, view) {
     make.top.inset(45)
     make.right.inset(15)
     make.width.equalTo(100)
     make.height.equalTo(0)
   }
 }

 const template = [{ // Video and Favorite                                                         
   type: "view",
   props: {
     bgcolor: $color("white"),
     radius: 7
   },
   views: [{
     type: "image",
     props: {
       id: "interface",
       radius: 5
     },
     layout: function(make, view) {
       var imgScale = 16 / 9;
       make.top.left.right.inset(10)
       make.height.equalTo(view.width).dividedBy(imgScale)
       //make.bottom.inset(55)
     }
   }, {
     type: "label",
     props: {
       id: "title",
       textColor: $color("#5c98f9"),
       font: $font(15)
     },
     layout: function(make, view) {
       make.top.equalTo($("interface").bottom).offset(5)
       make.left.right.inset(10)
     }
   }, {
     type: "button",
     props: {
       id: "time",
       titleColor: $color("black"),
       font: $font(13),
       bgcolor: $color("clear")
     },
     layout: function(make, view) {
       make.bottom.inset(4)
       make.left.inset(10)
     },
     events: {
       tapped(sender) {
         var preview = /https.*\//g.exec(sender.info) != false ? /https.*\//g.exec(sender.info)[0] + "preview.mp4" : false
         var cell = sender.super.super.super;
         var view = $("videos").runtimeValue();
         var indexPath = view.invoke("indexPathForCell", cell).rawValue();
         play(preview, indexPath, sender.info, "preview")

       }
     }
   }, {
     type: "label",
     props: {
       id: "like",
       textColor: $color("black"),
       font: $font(12),
     },
     layout: function(make, view) {
       make.bottom.inset(10)
       make.right.inset(10)
     }
   }, {
     type: "text",
     props: {
       id: "duration",
       textColor: $color("white"),
       bgcolor: $color("black"),
       alpha: 0.5,
       font: $font(12),
       radius: 3,
       align: $align.left,
       editable: false,
       scrollEnabled: false,
       insets: $insets(1, 1, 2, 1)
     },
     layout: function(make, view) {
       make.top.equalTo($("interface").bottom).offset(-23)
       make.right.equalTo($("interface").right).offset(-5)
     }
   }, {
     type: "text",
     props: {
       id: "hd",
       textColor: $color("black"),
       bgcolor: $color("#fcbc05"),
       text: "HD",
       alpha: 0.8,
       font: $font("bold", 12),
       radius: 3,
       align: $align.center,
       editable: false,
       scrollEnabled: false,
       insets: $insets(0, 0, 0, 0)
     },
     layout: function(make, view) {
       make.top.equalTo($("interface").top).offset(5)
       make.right.equalTo($("interface").right).offset(-5)
     }
   }, {
     type: "button",
     props: {
       id: "favorite",
       bgcolor: $color("clear"),
       //title: "🤔",
       font: $font(13),
     },
     layout: function(make, view) {
       make.top.equalTo($("like").top).offset(-3)
       make.left.equalTo($("time").right).offset(0)
       make.width.equalTo(30)
       make.height.equalTo(20)
     },
     events: {
       tapped(sender) {
         var info = sender.info;
         var cell = sender.super.super.super;
         var view = $("videos").runtimeValue();
         var index = view.invoke("indexPathForCell", cell).rawValue();
         var idx = index.row;
         favButtonTapped(sender, info, idx)
         if (contentMode == "Favorites") {
           $("search").placeholder = "共计 " + formatNum(LocalFavList.length) + " 部收藏";
           $("searchResult").text = "";
         }
       }
     }

   }, {
     type: "button",
     props: {
       id: "share",
       bgcolor: $color("clear"),
       font: $font(12),
       icon: $icon("022", $color("#777777"), $size(16, 16)),
       alpha: 1,
       //inset:$insets(0,0,0,0)
     },
     layout: function(make, view) {
       make.top.equalTo($("favorite").top).offset(2)
       make.right.equalTo($("like").left).offset(0)
       make.width.equalTo(30)
       make.height.equalTo(17)
     },
     events: {
       tapped(sender) {
         var url = "https://avgle.com/video/" + sender.info.vid
         $ui.menu({
           items: ["在 nplayer 中打开", "分享影片名字", "分享帖子链接", "分享影片链接"],
           handler: function(title, idx) {
             if (idx == 0) {
               videoUrlCatch(url, '', '', "nplayer")
             } else if (idx == 1) {
               $share.sheet(sender.info.name)
               //$clipboard.text = sender.info.name
             } else if (idx == 2) {
               $share.sheet(url)
             } else if (idx == 3) {
               $ui.loading(true);
               videoUrlCatch(url, '', '', "share")
             }
           },
         })

       }
     }

   }],
   layout: $layout.fill
 }]

 const templateC = [{ // Catagory and Collection
   type: "view",
   props: {
     bgcolor: $color("white"),
     radius: 7
   },
   views: [{
     type: "image",
     props: {
       id: "interface",
       radius: 5,
       bgcolor: $color("white")
     },
     layout: $layout.fill
   }, {
     type: "label",
     props: {
       id: "bottomLayer",
       textColor: $color("white"),
       bgcolor: $color("black"),
       alpha: 0.5,
     },
     layout: function(make, view) {
       make.left.right.bottom.inset(0)
       make.height.equalTo(30)
     }
   }, {
     type: "label",
     props: {
       id: "CCName",
       textColor: $color("white"),
       font: $font(16),
       alpha: 1,
     },
     layout: function(make, view) {
       make.bottom.inset(5)
       make.left.inset(10)
     }
   }, {
     type: "text",
     props: {
       id: "totalVideos",
       editable: "false",
       textColor: $color("white"),
       bgcolor: $color("#5c98f9"),
       font: $font("bold", 13),
       align: $align.center,
       scrollEnabled: false,
       lines: 1,
       insets: $insets(2, 2, 2, 2),
       radius: 10
     },
     layout: function(make, view) {
       make.bottom.inset(5)
       make.right.inset(10)
     }
   }, {
     type: "text",
     props: {
       id: "totalViews",
       editable: "false",
       textColor: $color("white"),
       bgcolor: $color("#5c98f9"),
       font: $font("bold", 13),
       align: $align.center,
       scrollEnabled: false,
       lines: 1,
       insets: $insets(2, 0, 2, 15),
       radius: 3,
     },
     layout: function(make, view) {
       make.top.inset(5)
       make.left.inset(10)
     }
   }, {
     type: "button",
     props: {
       id: "playButton",
       bgcolor: $color("clear"),
       icon: $icon("049", $color("white"), $size(15, 15)),
       alpha: 1,
     },
     layout: function(make, view) {
       make.top.inset(4)
       make.left.equalTo($("totalViews").right).offset(-18)
     },
     events: {
       tapped(sender) {
         $share.sheet(sender.info)
       }
     }
   }],
   layout: $layout.fill
 }]

 const info = {
   type: "view",
   props: {
     id: "preinfo",
     bgcolor: $color("#dddddd")
   },
   views: [{
     type: "text",
     props: {
       text: "Created by Nicked.\n\nSource from Avgle.\n\nhttps://t.me/nicked",
       bgcolor: $color("#dddddd"),
       textColor: $color("#aaaaaa"),
       font: $font(10),
       align: $align.center
     },

     layout: function(make, view) {
       make.top.inset(50)
       make.height.equalTo(100)
       make.width.equalTo($device.info.screen.width)
     }
   }, {
     type: "image",
     props: {
       src: nickIcon, //"https://i.loli.net/2017/11/06/59ffebf2eb071.jpeg",
       radius: 25,
       bgcolor: $color("#dddddd"),
       alpha: 0.8,
       align: $align.center,
     },
     layout: function(make, view) {
       make.size.equalTo($size(50, 50))
       make.top.inset(130)
       make.left.inset(130)
     }

   }, {
     type: "image",
     props: {
       src: avIcon, //"https://i.loli.net/2017/12/22/5a3cd0ff0b781.jpeg",
       radius: 25,
       bgcolor: $color("#dddddd"),
       alpha: 0.8,
       align: $align.center,
     },
     layout: function(make, view) {
       make.size.equalTo($size(50, 50))
       make.top.inset(130)
       make.right.inset(130)
     }

   }, {
     type: "text",
     props: {
       id: "loading",
       text: "Loading...",
       bgcolor: $color("#dddddd"),
       textColor: $color("#888888"),
       font: $font(20),
       align: $align.center
     },

     layout: function(make, view) {
       make.top.inset(200)
       make.height.equalTo(100)
       make.width.equalTo($device.info.screen.width)
     }
   }],
   layout: function(make, view) {
     make.top.equalTo($("search").bottom)
     make.left.right.bottom.inset(0)
   }
 }

 const nextPageView = {
   type: "button",
   props: {
     id: "next",
     bgcolor: $color("clear"),
     //icon: $icon("067", $color("#ffffff"), $size(25, 25))
   },
   layout: function(make, view) {
     make.top.bottom.inset(45)
     make.right.inset(0)
     make.width.equalTo(30)
   },
   events: {
     tapped(sender) {
       $ui.animate({
         duration: 0.5,
         animation: function() {
           $("page").alpha = 1
         },
       })
       page++;
       $("videos").data = [];
       getVideoData();
     }
   }
 }

 const prePageView = {
   type: "button",
   props: {
     id: "pre",
     bgcolor: $color("clear"),
     //icon: $icon("067", $color("#ffffff"), $size(25, 25))
   },
   layout: function(make, view) {
     make.top.bottom.inset(45)
     make.left.inset(0)
     make.width.equalTo(30)
   },
   events: {
     tapped(sender) {
       page--;
       has_next = true
       if (page == -1) {
         $ui.toast("已经在首页！", 1);
         page = 0;
         //has_next = false
         return
       }
       $ui.animate({
         duration: 0.5,
         animation: function() {
           $("page").alpha = 1
         },
       })
       $("videos").data = [];
       getVideoData();
     }
   }
 }

 const pageView = {
   type: "input",
   props: {
     id: "page",
     //type: $kbType.number,
     bgcolor: $color("white"),
     placeholder: "页码",
     font: $font(13),
     alpha: 0,
     align: $align.center
   },
   layout: function(make, view) {
     make.bottom.inset(15)
     make.height.equalTo(20)
     make.width.equalTo(88)
     make.centerX.inset(0)
   },
   events: {
     returned(sender) {
       sender.blur()
       if (isNaN(sender.text)) {
         $ui.alert("❌ 请输入数字！")
         return
       }
       if (sender.text < 0 || sender.text > totalPage) {
         $ui.alert("❌ 页码超出范围！")
         return
       }
       page = Math.floor(sender.text);
       $("videos").contentOffset = $point(0, 0);
       $("videos").data = [];
       has_next = true
       getVideoData();
       sender.text = ""
     }
   }
 }

 const nextPageButton = {
   type: "button",
   props: {
     id: "nextB",
     bgcolor: $color("white"),
     title: "下一页→",
     titleColor: $color("black"),
     font: $font(12),
     alpha: 0
   },
   layout: function(make, view) {
     make.bottom.inset(15)
     make.height.equalTo(20)
     make.width.equalTo(88)
     make.right.inset(20)
   },
   events: {
     tapped(sender) {
       page++;
       $("videos").data = [];
       getVideoData();

     }
   }
 }

 const prePageButton = {
   type: "button",
   props: {
     id: "preB",
     bgcolor: $color("white"),
     title: "←上一页",
     titleColor: $color("black"),
     font: $font(12),
     alpha: 0
   },
   layout: function(make, view) {
     make.bottom.inset(15)
     make.height.equalTo(20)
     make.width.equalTo(88)
     make.left.inset(20)
   },
   events: {
     tapped(sender) {
       page--;
       has_next = true
       if (page == -1) {
         $ui.toast("已经在首页！", 1);
         page = 0;
         return
       }
       $("videos").data = [];
       getVideoData();
     }
   }
 }

 const statusView = {
   type: "view",
   props: {
     bgcolor: $color("#dddddd"),
     id: "statusView",
   },
   views: [{
     type: "input",
     props: {
       id: "search",
       bgcolor: $color("#fdfdfd"),
       placeholder: "搜索影片中...",
       font: $font(15),
       clearButtonMode: 0
     },
     layout: function(make, view) {
       make.top.inset(10)
       make.height.equalTo(30)
       make.left.inset(60)
       make.right.inset(30)
     },
     events: {
       didBeginEditing: function(sender) {
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }

         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         $("search").runtimeValue().invoke("selectAll")
       },
       changed(sender) {
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
       },
       returned(sender) {
         contentMode = "Videos";
         cacheContent = "影片";
         $cache.set("cacheContent", cacheContent);

         sender.blur();
         
           $ui.pop()
           CCLevel = 1
         
         if (sender.text) {

           mode = "Search";
           var code = codeCorrectify(sender.text);
           if (code !== "none") {
             keyword = code;
             $("search").text = keyword
           } else {
             keyword = sender.text;
           }
           if (CCExist) {
             $("CCView").remove();
             CCExist = false;
             $("Avgle").add(VFView);
             VFExist = true;
             $("search").text = keyword
           }
           page = 0;
           has_next = true
           cacheFilters.Time = "a";
           cacheFilters.View = "bw"
           $cache.set("cacheFilters", cacheFilters)
           getVideoData()
         } else {
           if (CCExist) {
             $("CCView").remove();
             CCExist = false;
             $("Avgle").add(VFView);
             VFExist = true;
           }
           contentMode = "Videos";
           cacheContent = "影片";
           $cache.set("cacheContent", cacheContent);
           mode = "Videos";
           $("videos").contentOffset = $point(0, 0);
           $("videos").data = [];
           page = 0;
           has_next = true
           getVideoData();
         }
       }
     }
   }, {
     type: "button",
     props: {
       id: "filterButton",
       bgcolor: $color("#dddddd"),
       src: avgleIcon //"https://avgle.com/images/logo/logo.png"
     },
     layout: function(make, view) {
       make.top.inset(12)
       make.height.equalTo(25)
       make.width.equalTo(55)
       make.left.inset(0)
     },
     events: {
       tapped(sender) {
         //$ui.action(contentMode)
         $device.taptic(0)
         if (contentExist) {
           $("contentView").remove();
           contentExist = false
         }
         if (filterExist) {
           $("filterView").remove()
           filterExist = false;
           return
         }
         if (contentMode !== "Videos" && CCLevel == 1) {
           if (CCExist) {
             $("CCView").remove();
             CCExist = false
             $("Avgle").add(VFView);
             VFExist = true;
           }
           contentMode = "Videos";
           cacheContent = "影片";
           $cache.set("cacheContent", cacheContent);
           mode = "Videos"
           page = 0;
           has_next = true
           getVideoData();
           $("videos").contentOffset = $point(0, 0);
           //$ui.toast("", 0.1)
           return
         }
         if (CCLevel == 2) {
           if($("VFView"))
             $("VFView").add(filterView)
         } else {
           $("Avgle").add(filterView);
         }
         var data = []
         Object.keys(filters.Time).map(function(i) {
           data.push({
             filterLabel: {
               text: i,
               textColor: cacheFilters.Time == filters.Time[i] ? $color("white") : $color("black"),
               bgcolor: cacheFilters.Time == filters.Time[i] ? $color("tint") : $color("white")
             }
           })
         })
         $("filtersT").data = [{ title: "     时间线", rows: data }]
         data = []
         Object.keys(filters.View).map(function(i) {
           data.push({
             filterLabel: {
               text: i,
               textColor: cacheFilters.View == filters.View[i] ? $color("white") : $color("balck"),
               bgcolor: cacheFilters.View == filters.View[i] ? $color("tint") : $color("white")
             }
           })
         })
         $("filtersV").data = [{ title: "   影片类型", rows: data }]
         filterExist = true;
         $("filterView").updateLayout(function(make) {
           make.height.equalTo(145)
         });
       }
     }

   }, {
     type: "button",
     props: {
       id: "contentButton",
       bgcolor: $color("#dddddd"),
       icon: $icon("067", $color("#ffffff"), $size(25, 25))
     },
     layout: function(make, view) {
       make.top.inset(12)
       make.right.inset(0)
     },
     events: {
       tapped(sender) {
         $device.taptic(0)
         $ui.pop()
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false
           return
         }
           if (VFExist == true && CCLevel == 1) {
             $("VFView").add(contentView);
           } else {
             $("CCView").add(contentView)
           }
           CCLevel = 1
         var data = []
         content.map(function(i) {
           data.push({
             contentLabel: {
               text: i,
               textColor: cacheContent == i ? $color("white") : $color("balck"),
               bgcolor: cacheContent == i ? $color("tint") : $color("white")
             }
           })
         })
         $("contentList").data = data;
         contentExist = true
         //$ui.action(data)
         $("contentView").updateLayout(function(make) {
           make.height.equalTo(160)
         });
       }
     }

   }, {
     type: "label",
     props: {
       id: "searchResult",
       font: $font(14),
       textColor: $color("#cccccc"),
       text: ""
     },
     layout: function(make, view) {
       make.right.equalTo($("search").right).offset(-5)
       make.top.equalTo(17)
     }

   }],
   layout: function(make, view) {
     make.top.inset(0)
     make.left.right.inset(15)
     make.height.equalTo(45)
   }

 }
 var scale = 3

 var VFHeight = ($device.info.screen.width - 50) * 16 / 9 / scale + 55
 var CCHeight = ($device.info.screen.width - 50) * 16 / 9 / scale

 const VFView = { // Video and Favorite
   type: "view",
   props: {
     id: "VFView",
     bgcolor: $color("#dddddd"),
     title: "Avgle"
   },
   views: [statusView, info, {
     type: "matrix",
     props: {
       id: "videos",
       itemHeight: VFHeight,
       columns: 1,
       spacing: 15,
       square: false,
       bgcolor: $color("clear"),
       template: template,
     },
     layout: function(make, view) {
       make.left.right.bottom.inset(0)
       make.top.equalTo($("statusView").bottom).offset(0)
     },
     events: {
       didSelect(sender, indexPath, data) {
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         if(data.share.info.vidUrl){
           var url = data.share.info.vidUrl
         } else{
           var url = "https://avgle.com/video/" + data.share.info.vid;
         }
        
         if($("playerMode").index == 1){
           avgleWeb(url)
           return
         }
           
         play(url, indexPath, data.interface.src, "video");
       },
       didReachBottom(sender) {
         sender.endFetchingMore();
         //$ui.toast("🙈 已经到底了",1)
         $ui.animate({
           duration: 0.2,
           animation: function() {
             $("preB").alpha = 1;
             $("nextB").alpha = 1;
             $("page").alpha = 1;
           },
         })
         return
       },
       pulled(sender) {
         if (filterExist) {
           $("filterView").remove()
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         page = 0;
         has_next = true
         $("videos").data = [];
         if (contentMode == "Favorites" && mode == "Videos") {
           $("search").placeholder = "共计 " + formatNum(LocalData.favorite.length) + " 部收藏";
           $("searchResult").text = "";
           var temp = LocalFavList;
           tempList = [];
           tempData = { "favorite": [] };
           temp.map(function(i) {
             getFavoriteData(i)
           });
           $ui.toast("更新成功！", 1)
         } else {
           getVideoData();
           $ui.toast("更新成功！", 1);
         }
         $("videos").endRefreshing();
       },
       willBeginDragging(sender) {
         startY = sender.contentOffset.y;
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         $ui.animate({
           duration: 0.5,
           animation: function() {
             $("preB").alpha = 0;
             $("nextB").alpha = 0;
             $("page").alpha = 0;
           },
         })
       },
       didEndDragging(sender) {
         endY = sender.contentOffset.y;
         if (Math.abs(endY - startY) > 150) {

           delPlayer()
           $ui.loading(false)
         }

       }

     }
   }, {
      type: "tab",
      props: {
        id: "playerMode",
        items: ["内嵌", "网页"],
        tintColor: $color("tint"),
        radius: 5,
        bgcolor: $color("white"),
        hidden: false,
        alpha: 0.8,
        index: $cache.get("playerMode") || 0
      },
      layout: function(make) {
        make.centerX.equalTo()
        make.bottom.inset(40)
        make.height.equalTo(22)
      },
      events: {
        changed(sender) {
          if (sender.index == 0) $cache.set("playerMode",0);
          else $cache.set("playerMode",1);
          
        }
      }
    },nextPageView, prePageView, pageView, nextPageButton, prePageButton],
   layout: $layout.fill
 }

 const CCView = { // category and collection
   type: "view",
   props: {
     id: "CCView",
     bgcolor: $color("#dddddd"),
   },
   views: [statusView, info, {
     type: "matrix",
     props: {
       id: "CCList",
       itemHeight: CCHeight,
       columns: 1,
       spacing: 15,
       square: false,
       bgcolor: $color("clear"),
       template: templateC,
     },
     layout: function(make, view) {
       make.left.right.bottom.inset(0)
       make.top.equalTo($("statusView").bottom).offset(0)
     },
     events: {
       didSelect(sender, indexPath, data) {
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         CCLevel = 2
         VFExist = true;
         cacheFilters.Time = "a";
         cacheFilters.View = "bw";
         $cache.set("cacheFilters", cacheFilters);         
         $ui.push({
           type: "view",
           props: {
             bgcolor: $color("#dddddd"),
             title: "Avgle"
           },
           views: [VFView,
           ]
         })
         page = 0;
         has_next = true
         $("videos").data = [];
         $("videos").contentOffset = $point(0, 0);
         if (contentMode == "Categories") {
           //contentMode = "Videos";
           mode = "Cat";
           CHID = data.info;
           var name = /(.*?)・.*/g.exec(data.CCName.text);
           if (!name) {
             catName = data.CCName.text
           } else {
             catName = name[1]
           }
           getVideoData();
         } else if (contentMode == "Collections") {
           //contentMode = "Videos";
           mode = "Search";
           keyword = data.info;
           $("search").text = data.info;
           getVideoData();
         }
       },
       didReachBottom(sender) {
         sender.endFetchingMore();
         if (contentMode == "Collections") {
           getCollectionData();
         } else {
           $ui.toast("🙈 已经到底了")
           return
         }
       },
       pulled(sender) {
         cpage = -1;
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }
         $("search").text = "";
         if (contentMode == "Categories") {
           $("CCList").data = [];
           getCategoryData();
         } else if (contentMode == "Collections") {
           $("CCList").data = [];
           getCollectionData();
         }
         $ui.toast("更新成功！", 0.1);

         $("CCList").endRefreshing();

       },
       willBeginDragging(sender) {
         startY = sender.contentOffset.y;
         if (filterExist) {
           $("filterView").remove();
           filterExist = false;
         }
         if (contentExist) {
           $("contentView").remove();
           contentExist = false;
         }

       },
       didEndDragging(sender) {
         endY = sender.contentOffset.y;
         if (Math.abs(endY - startY) > 180) {

           delPlayer()
           $ui.loading(false)
         }
       }

     }
   }, ],
   layout: $layout.fill
 }

 function getVideoData() {
   delPlayer()
   $ui.loading(true)
   $("loading").text = "Loading..."
   $ui.toast("载入中...", 10)
   if (mode == "Cat") {
     url = "https://api.avgle.com/v1/videos/" + page + "?limit=10&c=" + CHID + "&t=" + cacheFilters.Time + "&o=" + cacheFilters.View;
   } else {
     if (mode == "Search") {
       url = "https://api.avgle.com/v1/search/" + encodeURI(keyword) + "/" + page + "?limit=10&t=" + cacheFilters.Time + "&o=" + cacheFilters.View
     } else {
       url = "https://api.avgle.com/v1/videos/" + page + "?limit=10&t=" + cacheFilters.Time + "&o=" + cacheFilters.View;
     }
   }
   $http.request({
     url: url,
     timeout: 5,
     handler: function(resp) {
       //$ui.action(resp.error)
       //$ui.action(resp.error.indexOf("请求超时"))
       var success = resp.data.success;
       if (!success || !resp.response) {
         $ui.toast("", 0.1)
         $ui.alert("❌ 网络连接出错！");
         return
       }
       var video_num = resp.data.response.total_videos
       if (video_num == 0) {
         $ui.toast("", 0.1)
         $ui.alert("❌ 没有搜索结果！");
         $ui.loading(false);
         $("VFView").remove()
         $("Avgle").add(VFView);
         $("search").text = encodeURI(keyword);
         $("search").placeholder = "重新搜索"
         $("searchResult").text = filterName[cacheFilters.Time] + "找到 0" + " 部影片";
         $("loading").text = ""
         return
       }
       $("videos").data = []
       $("videos").contentOffset = $point(0, 0);
       if (!resp.data.response.has_more) {
         if (has_next == false) {
           $ui.toast("🙈 已经到底了", 1);
           page--;
           $ui.loading(false);
           return
         }
         has_next = false
       }
       var infos = resp.data.response.videos;
       //$ui.action(infos)
       infos.map(function(i) {
         $("videos").data = $("videos").data.concat({
           interface: {
             src: i.preview_url
           },
           title: {
             text: i.title
           },
           time: {
             title: formatTime(i.addtime),
             info: i.preview_url
           },
           duration: {
             text: formatDuration(i.duration)
           },
           like: {
             text: "❤️ " + formatNum(i.likes) + " 🖤 " + formatNum(i.dislikes) + " ▶️ " + formatNum(i.viewnumber),
             alpha: 0.7,
           },
           hd: {
             hidden: i.hd == true ? false : true
           },
           favorite: {
             title: LocalFavList.indexOf(i.vid) > -1 ? "😍" : "🤔",
             alpha: LocalFavList.indexOf(i.vid) > -1 ? 1 : 0.6,
             info: {
               title: i.title,
               image: i.preview_url,
               time: formatTime(i.addtime),
               duration: formatDuration(i.duration),
               like: "❤️ " + i.likes + " 🖤 " + i.dislikes + " ▶️ " + i.viewnumber,
               hd: i.hd == true ? false : true,
               vid: i.vid,
               vidUrl:i.video_url
             }
           },
           share: {
             info: { vid: i.vid, name: i.title,vidUrl:i.video_url}
           }
         })
       })
       $ui.toast("", 0.1);
       $ui.loading(false);
       $("loading").text = ""
       if (mode == "Search") {
         $("searchResult").text = filterName[cacheFilters.Time] + "找到 " + formatNum(video_num) + " 部影片";
         $("search").placeholder = "";
       } else {
         if (mode == "Cat") {
           $("search").placeholder = "#" + catName + " | " + filterName[cacheFilters.Time] + " " + formatNum(video_num) + " 部影片 ";
         } else {
           $("search").placeholder = filterName[cacheFilters.Time] + " " + formatNum(video_num) + " 部影片 ";
         }
         $("searchResult").text = "";
       }
       $("videos").contentOffset = $point(0, 0);
       totalPage = Math.floor(video_num / 10)
       $("page").placeholder = page + " / " + totalPage
     }
   })
 }

 function getFavoriteData(vid) {
   delPlayer()
   url = "https://api.avgle.com/v1/video/" + vid;
   $("searchResult").text = "";
   $http.request({
     url: url,
     handler: function(resp) {
       var success = resp.data.success;
       if (!resp.response) {
         $ui.alert("❌ 网络连接出错！");
         //$ui.toast("",0.1)
         return
       }
       if (!success) {
         $ui.alert(url + "❌ 该影片连接出错！\n请稍候再试。")
         return
       }

       var i = resp.data.response.video;
       var info = {
         title: i.title,
         image: i.preview_url,
         time: formatTime(i.addtime),
         duration: formatDuration(i.duration),
         like: "❤️ " + i.likes + " 🖤 " + i.dislikes + " ▶️ " + i.viewnumber,
         hd: i.hd == true ? false : true,
         vid: i.vid,
         vidUrl:i.video_url
       };
       $("videos").data = $("videos").data.concat({
         interface: {
           src: i.preview_url
         },
         title: {
           text: i.title
         },
         time: {
           title: formatTime(i.addtime),
           info: i.preview_url
         },
         duration: {
           text: formatDuration(i.duration)
         },
         like: {
           text: "❤️ " + i.likes + " 🖤 " + i.dislikes + " ▶️ " + i.viewnumber,
           alpha: 0.7,
         },
         hd: {
           hidden: i.hd == true ? false : true
         },
         favorite: {
           title: "😍",
           alpha: 1,
           info: info
         },
         share: {
           info: { vid: i.vid, name: i.title}
         }
       });
       if (contentMode == "Favorites") {
         tempList.push(i.vid);
         tempData.favorite.push(info)
         if (tempList.length == LocalFavList.length) {
           LocalFavList = tempList;
           LocalData = tempData;
           writeCache();
         }
       };

     }
   })
 }

 function getCollectionData() {
   delPlayer()
   $ui.loading(true)
   $("loading").text = "Loading..."
   $("searchResult").text = "";
   cpage++;
   $http.request({
     url: "https://api.avgle.com/v1/collections/" + cpage + "?limit=10",
     timeout: 5,
     handler: function(resp) {
       var success = resp.data.success;
       if (!success || !resp.response) {
         $ui.alert("❌ 网络连接出错！");
         return
       }
       if (!resp.data.response.has_more && cpage > 0) {
         $ui.toast("🙈 已经到底了", 1);
         $ui.loading(false);
         //return
       }
       var collections = resp.data.response.collections;
       collections.map(function(i) {
         $("CCList").data = $("CCList").data.concat({
           interface: {
             src: i.cover_url
           },
           CCName: {
             text: i.title
           },
           totalVideos: {
             text: i.video_count.toString()
           },
           totalViews: {
             text: formatNum(i.total_views),
             hidden: false,
           },
           playButton: {
             hidden: false,
             info: i.collection_url
           },
           info: i.keyword

         })
       })
       $("search").text = ""
       $("search").placeholder = "共计 " + formatNum(resp.data.response.total_collections) + " 个合集"
       $ui.loading(false)
       $("loading").text = ""
     }
   })

 }

 function getCategoryData() { // category and collection
   delPlayer()
   $ui.loading(true)
   $("loading").text = "Loading..."
   $("searchResult").text = "";
   url = "https://api.avgle.com/v1/categories"
   $http.request({
     url: url,
     timeout: 5,
     handler: function(resp) {
       var success = resp.data.success;
       if (!success || !resp.response) {
         $ui.alert("❌ 网络连接出错！");
         return
       }
       var categories = resp.data.response.categories
       $("CCList").data = []
       categories.map(function(i) {
         $("CCList").data = $("CCList").data.concat({
           interface: {
             src: [i.cover_url.slice(0,8),"static.",i.cover_url.slice(8)].join("")
           },
           CCName: {
             text: i.name
           },
           totalVideos: {
             text: formatNum(i.total_videos)
           },
           totalViews: {

             hidden: true,
           },
           playButton: {
             hidden: true
           },
           info: i.CHID
         })
       })
       $("search").text = ""
       $("search").placeholder = "搜索番号或者名字"
       $ui.loading(false)
       $("loading").text = ""
     }
   })
 }

 function favButtonTapped(sender, info, idx) {
   if (sender.title == "🤔") {
     if ($("player")) {
       sender.title = "😍"
     } else {
       var data = $("videos").data;
       data[idx].favorite.title = "😍";
       data[idx].favorite.alpha = 1;
       $("videos").data = data;
     }
     LocalData.favorite.unshift(info);
     LocalFavList.unshift(info.vid)
     writeCache();
     $ui.toast("😍 已收藏！", 1)
   } else if (sender.title == "😍") {
     if ($("player")) {
       sender.title = "🤔"
     } else {
       var data = $("videos").data;
       data[idx].favorite.title = "🤔";
       data[idx].favorite.alpha = 1;
       $("videos").data = data;
     }
     var idxx = LocalFavList.indexOf(info.vid);
     LocalFavList.splice(idxx, 1);
     LocalData.favorite.splice(idxx, 1);
     writeCache()
     $ui.toast("🤔 已取消！", 1)
   }
 }

 function formatDuration(ns) {
   var mins = Math.floor(ns / 60)
   var hours = mins > 60 ? Math.floor(mins / 60) : 0
   var seconds = Math.floor(((ns / 60) - mins) * 60)
   if (hours > 0) {
     mins = mins - 60 * hours
   }
   mins = mins.toString().length > 1 ? mins : `0${mins}`
   hours = hours.toString().length > 1 ? hours : `0${hours}`
   seconds = seconds.toString().length > 1 ? seconds : `0${seconds}`
   if (hours == "00") {
     return `${mins}:${seconds}`
   } else {
     return `${hours}:${mins}:${seconds}`
   }

 }

 function formatTime(ns) {
   var myTime = Math.floor(new Date() / 1000);
   var timeDiff = myTime - ns
   if (timeDiff / 60 < 60) {
     return Math.floor(timeDiff / 60) + " 分钟前"
   } else if (timeDiff / 3600 < 24) {
     return Math.floor(timeDiff / 3600) + " 小时前"
   } else {
     return Math.floor(timeDiff / 3600 / 24) + " 天前"
   }

 }

 function formatNum(num) {
   var num = (num || 0).toString(),
     result = '';
   while (num.length > 3) {
     result = ',' + num.slice(-3) + result;
     num = num.slice(0, num.length - 3);
   }
   if (num) { result = num + result; }
   return result;
 }

 function writeCache() {
   $file.write({
     data: $data({ string: JSON.stringify(LocalData) }),
     path: LocalDataPath
   })
 }

 // function clipboardDetect() {
 //   var str = $clipboard.text
 //   if (!str || str.length > 200) {
 //     return "none"
 //   } else {
 //     var reg = /[sS][nN][iI][sS][\s\-]?\d{3}|[aA][bB][pP][\s\-]?\d{3}|[iI][pP][zZ][\s\-]?\d{3}|[sS][wW][\s\-]?\d{3}|[jJ][uU][xX][\s\-]?\d{3}|[mM][iI][aA][dD][\s\-]?\d{3}|[mM][iI][dD][eE][\s\-]?\d{3}|[mM][iI][dD][dD][\s\-]?\d{3}|[pP][gG][dD][\s\-]?\d{3}|[sS][tT][aA][rR][\s\-]?\d{3}|[eE][bB][oO][dD][\s\-]?\d{3}|[iI][pP][tT][dD][\s\-]?\d{3}|[cC][hH][nN][\s\-]?\d{3}/g;
 //     var match = str.match(reg);
 //     if (match) {
 //       var detect = /([a-zA-Z]{3,5})[\s\-]?(\d{3,4})/g.exec(match[0])
 //       var keyword = detect[1] + "-" + detect[2]
 //       if(keyword.length > 20){
 //         return "none"
 //       }else{
 //         return keyword
 //       }
 //     } else {
 //       return "none"
 //     }
 //   }
 // }

 function clipboardDetect() {
   let str = ""
   if ($clipboard.text)
     str = $clipboard.text
   var reg1 = /[sS][nN][iI][sS][\s\-]?\d{3}|[aA][bB][pP][\s\-]?\d{3}|[iI][pP][zZ][\s\-]?\d{3}|[sS][wW][\s\-]?\d{3}|[jJ][uU][xX][\s\-]?\d{3}|[mM][iI][aA][dD][\s\-]?\d{3}|[mM][iI][dD][eE][\s\-]?\d{3}|[mM][iI][dD][dD][\s\-]?\d{3}|[pP][gG][dD][\s\-]?\d{3}|[sS][tT][aA][rR][\s\-]?\d{3}|[eE][bB][oO][dD][\s\-]?\d{3}|[iI][pP][tT][dD][\s\-]?\d{3}|[iI][pP][xX][\s\-]?\d{3}/g;
   var reg2 = /[a-zA-Z]{3,5}[\s\-]?\d{3,4}/g;
   var match = str.match(reg1);
   if (match) {
     var detect = /([a-zA-Z]{3,5})[\s\-]?(\d{3,4})/g.exec(match[0])
     var keyword = detect[1] + "-" + detect[2]
     if (keyword.length > 20) {
       return "none"
     } else {
       return keyword
     }
   } else {
     var match = str.match(reg2);
     if (match) {
       var detect = /([a-zA-Z]{3,5})[\s\-]?(\d{3,4})/g.exec(match[0])
       let heyzo = /[Hh][Ee][Yy][Zz][Oo]/g.exec(detect[1])
       if (heyzo) {
         var keyword = detect[1] + " " + detect[2]
       } else {
         var keyword = detect[1] + "-" + detect[2]
       }
       if (keyword.length > 20) {
         return "none"
       } else {
         return keyword
       }
     } else {
       return "none"
     }

   }

 }

 function codeCorrectify(detect) {
   if (!detect) {
     return "none"
   } else {
     var s = /([a-zA-Z]{3,5})[\s\-]?(\d{3,4})/g.exec(detect)
     if (s) {
       return s[1] + "-" + s[2]
     } else {
       return detect
     }
   }

 }

 // async function getInfo(url) {
 //
 //   return new Promise(resolve => {
 //     $('VFView').add({
 //       type: 'web',
 //       props: {
 //         id: 'avgle_web',
 //         url: url,
 //         hidden: true,
 //       },
 //       layout: $layout.fill,
 //       //            layout (make) {
 //       //                make.size.equalTo($size(0, 0));
 //       //            },
 //       events: {
 //         didFinish(sender) {
 //           sender.eval({ script: "setInterval(function(){$('.vjs-big-play-button').click();},10);" });
 //
 //         },
 //         didSendRequest(request) {
 //
 //           if (!/video-url\.php/.test(request.url)) {
 //             return
 //           };
 //           let vInfo = {},
 //             params = request.url.match(/\?(.*)/)[1];
 //
 //           resolve(params);
 //           $('avgle_web').remove();
 //         }
 //       }
 //     });
 //   });
 // }
 //
 function videoUrlCatch(url, indexPath, poster, addPlayer) {
   OriUrl = url
   $ui.toast("影片获取中…", 20);
   let videoUrl = false
   $('VFView').add({
     type: 'web',
     props: {
       id: 'avgle_web',
       url: encodeURI(url),
       hidden: true,
       ua:"Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"
     },
     layout: $layout.fill,
     //            layout (make) {
     //                make.size.equalTo($size(0, 0));
     //            },
     events: {
       didFinish(sender) {
         sender.eval({ script: "setInterval(function(){$('.vjs-big-play-button').click();},10);" });

       },
       didSendRequest(request) {
         //$ui.alert(request.url)       
         if (!/video-url\.php/.test(request.url)) {        
           return
         }         
         let vInfo = {},
         params = request.url.match(/\?(.*)/)[1];
         $('avgle_web').remove();
         Status = "closed"
         $http.get({
           url: `https://avgle.com/video-url.php?${params}`,
           header:{
  'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1',
},
           timeout: 5,
           handler: function(resp) {
             videoUrl = resp.data.url
             //$ui.alert(videoUrl)
             $http.get({
               url: videoUrl,
               handler: function(resp) {
                 $ui.loading(false);
                 var data = resp.data
                 if(data == "Wrong key"){
                   WrongKey(OriUrl, Status)
                   Status = "Opened"
                   return
                 } else{
                  if (!videoUrl) {
                    $ui.alert("❌ 影片加载失败！")
                    return
                  }
                  $ui.toast("", 0.1)
                  if (typeof addPlayer === 'function') {
                    addPlayer(videoUrl, indexPath, poster);
                  } else if (addPlayer == "share") {
                    $share.sheet(videoUrl)
                  } else {      
                    $app.openURL("nplayer-" + videoUrl)
                  } 
                  return
                 }
               }
             })
           }
         })
       }
     }
   });
   $delay(20, function() {
     if (videoUrl == false) {
      WrongKey(OriUrl,Status)
      return
     }
   })
 }

 function avgleWeb(url) {
   $ui.push({
     props: {},
     views: [{
       type: "web",
       props: {
         id: "web",
         transparent: true,
         showsProgress: true,
         url: encodeURI(url),
         ua:"Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1",
//         script:function(){
//           let cookie = document.cookie
//           $notify("cc",{
//             "cookie":cookie
//           })
//         }

       },
//       events: {
//         cc:function o {
//           $ui.alert(o)      
//           }
//       },    
       layout: $layout.fill,

     }]
   });
 }

 function WrongKey(url,Status){
  if(Status!=="Opened"){
    $ui.toast("",0.1)
    $ui.alert({
      title: "需要验证",
      message: "请先用弹出的网页版完成一次验证播放再返回用脚本进行播放(若碰到连续校验，建议开启网页播放模式)",
      actions: [{
          title: "确定",
          handler: function() {
            return
          }
        },
        {
          title: "Cancel",
          handler: function() {
            return
          }
        }
      ]
    })
    avgleWeb(url)
  }

 }

 //async function videoUrlCatch(url, indexPath, poster, addPlayer){
 //  $ui.toast("影片获取中…", 20);
 //     let params = await getInfo(url);
 //    $http.get({
 //        url: `https://avgle.com/video-url.php?${params}`,
 //       timeout:5,
 //       handler:function(resp){
 //         var videoUrl = resp.data.url
 //         $ui.loading(false);
 //         if (!videoUrl) {
 //           $ui.alert("❌ 影片加载失败！")
 //           return
 //         }
 //         $ui.toast("",0.1)
 //         if (typeof addPlayer === 'function') { 
 //           addPlayer(videoUrl,indexPath, poster);
 //        }else if(addPlayer == "share"){
 //          $share.sheet(videoUrl)
 //        }else {
 ////          $ui.action("dd")        
 //          $app.openURL("nplayer-"+videoUrl)
 //        }
 //       }
 //     })
 //
 //}
 //
 function addPlayer(videoUrl, indexPath, poster) {
   delPlayer()

   $("videos").cell(indexPath).add({
     type: "video",
     props: {
       id: "player",
       src: videoUrl,
       poster: poster,
       ua:"Mozilla/5.0 (iPhone; CPU iPhone OS 12_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1"

     },
     layout: function(make, view) {
       var scale = 16 / 9;
       make.top.left.right.inset(0)
       make.height.equalTo(view.width).dividedBy(scale);
     }
   });
   $delay(0.5, function() {
     $("player").play()
   })
 }

function delPlayer() {
  if ($("player")) {
    $("player").pause();
    $("player").stopLoading();
    $("player").remove();
  }
}

function play(url, indexPath, poster, videoMode) {
   $ui.loading(true);
   if (videoMode == "video") {
     videoUrlCatch(url, indexPath, poster, addPlayer)
   } else if (videoMode == "preview") {
     addPlayer(url, indexPath, poster, videoMode)
   }

 }
function wechatPay() {
  $ui.alert({
    title: "确定赞赏？",
    message:
      "点击确定二维码图片会自动存入相册同时会跳转至微信扫码,请选择相册中的二维码图片进行赞赏。",
    actions: [
      {
        title: "确定",
        handler: function() {
          let payUrl = "weixin://scanqrcode";
          $ui.toast("赞赏码下载中...")
          $http.download({
            url:
              "https://raw.githubusercontent.com/nicktimebreak/xteko/master/JavBus/wechat.jpg",
            progress: function(bytesWritten, totalBytes) {
              var percentage = (bytesWritten * 1.0) / totalBytes;
            },
            handler: function(resp) {
              $photo.save({
                data: resp.data,
                handler: function(success) {
                  if (success) {
                    $push.schedule({
                      title: "二维码已存入相册",
                      body: "点击右侧「相册」选取",
                      delay: 0.8
                    });
                    $app.openURL(payUrl);
                  }
                }
              });
            }
          });
        }
      },
      {
        title: "取消",
        handler: function() {}
      }
    ]
  });
}

// function wechatPay() {
//   $ui.alert({
//     title: "确定赞赏？",
//     message: "点击确定二维码图片会自动存入相册同时会跳转至微信扫码,请选择相册中的二维码图片进行赞赏。",
//     actions: [{
//         title: "确定",
//         handler: function() {
//           let payUrl = "weixin://scanqrcode"
//           $http.download({
//             url: "https://raw.githubusercontent.com/nicktimebreak/xteko/master/JavBus/wechat.jpg",
//             progress: function(bytesWritten, totalBytes) {
//               var percentage = bytesWritten * 1.0 / totalBytes
//             },
//             handler: function(resp) {
//               $photo.save({
//                 data: resp.data,
//                 handler: function(success) {
//                   if (success) {
//                     $app.openURL(payUrl)
//                   }
//                 }
//               })
//             }
//           })
//         }
//       },
//       {
//         title: "取消",
//         handler: function() {
//
//         }
//       }
//     ]
//   })
// }
//
 function tutorial() {
   var text = "提示\n- 点击片名左下角影片上传时间预览 8 秒影片，拒绝封杀；\n- 点击片名左下角 🤔 收藏影片；\n- 点击收藏右侧按钮可分享影片名字链接；\n- 点击预览图片或下方标题播放完整影片；\n- 点击屏幕右边缘向下翻页，左边缘向上翻页,或滑至底部出现翻页按钮；\n- 点击左上角 Avgle Logo 可筛选影片；\n- 请合理安排娱乐与工作时间, 劳逸结合，强身健体。"

   // Views
   var hintView = $objc("BaseHintView").invoke("alloc").invoke("initWithText", text)
   var textView = hintView.invoke("subviews").invoke("objectAtIndex", 1).invoke("subviews").invoke("objectAtIndex", 1)

   // Attribute for text
   var string = $objc("NSMutableAttributedString").invoke("alloc").invoke("initWithString", text)
   string.invoke("addAttribute:value:range:", "NSFont", $font("bold", 26), $range(0, 2))
   string.invoke("setAlignment:range:", $align.center, $range(0, 2))

   string.invoke("addAttribute:value:range:", "NSFont", textView.invoke("font"), $range(2, string.invoke("length") - 2))
   string.invoke("addAttribute:value:range:", "NSColor", $color("tint"), $range(text.indexOf("收藏"), 2))
   string.invoke("addAttribute:value:range:", "NSColor", $color("tint"), $range(text.indexOf("分享"), 2))
   string.invoke("addAttribute:value:range:", "NSColor", $color("tint"), $range(text.indexOf("预览"), 2))

   // Paragraph Style
   var para = $objc("NSMutableParagraphStyle").invoke("alloc.init")
   para.invoke("setParagraphSpacing", 15)
   para.invoke("setAlignment", $align.left)

   string.invoke("addAttribute:value:range:", "NSParagraphStyle", para, $range(2, string.invoke("length") - 2))

   // Setup
   textView.invoke("setAttributedText", string)

   // Show View
   hintView.invoke("show")

 }
 const checkAdultView = {
   type: "view",
   props: {
     id: "checkAdult",
     bgcolor: $color("black")
   },
   views: [{
     type: "text",
     props: {
       text: "FBI WARNING",
       textColor: $color("white"),
       font: $font("Helvetica-Bold", 25),
       bgcolor: $color("red"),
       insets: $insets(5, 0, 0, 0),
       align: $align.center,
       editable: false
     },
     layout: function(make, view) {
       make.top.inset(55)
       make.left.right.inset(90)
       make.height.equalTo(40)
     }
   }, {
     type: "text",
     props: {
       text: "Federal law provides severe civil and criminal penalties for the unauthorized reproduction, distribution, or exhibition of copyrighted motion pictures (Title 17, United States Code, Sections 501 and 508). The Federal Bureau of Investigation investigates allegations of criminal copyright infringement (Title 17, United States Code, Section 506).",
       textColor: $color("white"),
       font: $font("bold", 14),
       bgcolor: $color("clear"),
       insets: $insets(0, 0, 0, 0),
       align: $align.justified,
       editable: false
     },
     layout: function(make, view) {
       make.top.inset(120)
       make.left.right.inset(10)
       make.height.equalTo(160)
     }
   }, {
     type: "text",
     props: {
       text: "警告 ⚠️",
       textColor: $color("white"),
       font: $font("Helvetica-Bold", 25),
       bgcolor: $color("red"),
       insets: $insets(5, 0, 0, 0),
       align: $align.center,
       editable: false
     },
     layout: function(make, view) {
       make.top.inset(280)
       make.left.right.inset(130)
       make.height.equalTo(40)
     }
   }, {
     type: "text",
     props: {
       text: "本脚本运行内容包含成人影片、图片，可能会引起你的不适，请谨慎运行。\n未满十八岁，禁止运行。\n\n脚本运行需代理，请将 https://avgle.com 加入代理。\n关于使用说明，请打开脚本代码看首段注释。",
       textColor: $color("white"),
       font: $font("bold", 14),
       bgcolor: $color("clear"),
       insets: $insets(0, 0, 0, 0),
       align: $align.center,
       editable: false
     },
     layout: function(make, view) {
       make.top.inset(350)
       make.left.right.inset(10)
       make.height.equalTo(160)
     }
   }, {
     type: "button",
     props: {
       title: "已满十八岁",
       titleColor: $color("black"),
       bgcolor: $color("white")
     },
     layout: function(make, view) {
       make.left.right.inset(120)
       make.bottom.inset(100)
       make.height.equalTo(30)
     },
     events: {
       tapped: function(sender) {
         $("checkAdult").remove()
         $cache.set("ADULT", true)
       }
     }
   }, {
     type: "button",
     props: {
       title: "未满十八岁",
       titleColor: $color("white"),
       bgcolor: $color("red")
     },
     layout: function(make, view) {
       make.left.right.inset(120)
       make.bottom.inset(40)
       make.height.equalTo(30)
     },
     events: {
       tapped: function(sender) {
         $app.close()
       }
     }
   }],
   layout: $layout.fill
 }

 function initial() {
   var current = $addin.current;
   current.author = "Nicked";
   current.website = "https://t.me/nicked";
   current.version = version;

   if ($file.read(LocalDataPath)) {
     LocalData = JSON.parse($file.read(LocalDataPath).string);
     LocalFavList = LocalData.favorite.map(i => i.vid);

   } else {
     LocalData = { "favorite": [] };
     LocalFavList = [];
   };
   /*cacheFilters = $cache.get("cacheFilters") || { "Time": "a", "View": "bw" };*/
   cacheFilters = { "Time": "a", "View": "bw" };
   cacheContent = "影片";
   contentExist = false;
   filterExist = false;
   contentMode = "Videos";
   //$("Avgle").add(VFView)
   VFExist = true; // videos and favorites
   CCExist = false; // categories and collections 
   page = 0;
   has_next = true;
   $app.autoKeyboardEnabled = true
   CCLevel = 1
   Status = "Closed"
 }


//检测扩展更新
function scriptVersionUpdate() {
  $http.get({
    url:
      "https://raw.githubusercontent.com/nicktimebreak/xteko/master/Avgle/updateInfo",
    handler: function(resp) {
      var afterVersion = resp.data.version;
      var msg = resp.data.msg;
      if (afterVersion > version) {
        $ui.toast("检测到脚本更新,下载中...");
        // var url = "jsbox://install?url=https://raw.githubusercontent.com/nicktimebreak/xteko/master/JavBus/JavBus.js&name=JavBus&icon=icon_087.png&types=1&author=Nicked&website=https://t.me/nicked";
        // $app.openURL(encodeURI(url));
        // $app.close()

        $http.download({
          url:
            "https://raw.githubusercontent.com/nicktimebreak/xteko/master/Avgle/Avgle.js",
          handler: resp => {
            let box = resp.data;
            $addin.save({
              name: $addin.current.name,
              data: box,
              version: afterVersion,
              author: "Nicked",
              icon: "icon_087",
              handler: success => {
                if (success) {
                  $device.taptic(2);
                  $delay(0.2, function() {
                    $device.taptic(2);
                  });

                  $ui.alert({
                    title: "更新已完成",
                    message:"本次更新内容:\n"+msg,
                    actions: [
                      {
                        title: "OK",
                        handler: function() {
                          $addin.restart();
                        }
                      }
                    ]
                  });
                }
              }
            });
          }
        });
      }
    }
  });
}



// function scriptVersionUpdate() {
//   $http.get({
//     url: "https://raw.githubusercontent.com/nicktimebreak/xteko/master/Avgle/updateInfo",
//     handler: function(resp) {
//       var afterVersion = resp.data.version;
//       var msg = resp.data.msg;
//       if (afterVersion > version) {
//         $ui.alert({
//           title: "检测到新的版本！V" + afterVersion,
//           message: "更新后请至扩展列表启动新版本。\n" + msg,
//           actions: [{
//             title: "更新",
//             handler: function() {
//               var url = "jsbox://install?url=https://raw.githubusercontent.com/nicktimebreak/xteko/master/Avgle/Avgle.js&name=Avgle" + afterVersion + "&icon=icon_135.png&types=1&version=" + afterVersion + "&author=Nicked&website=https://t.me/nicked";
//               $app.openURL(encodeURI(url));
//               $app.close()
//             }
//           }, {
//             title: "取消"
//           }]
//         })
//       }
//     }
//   })
// }
//
 function alert() {
   $http.get({
     url: "https://raw.githubusercontent.com/nicktimebreak/xteko/master/Avgle/alert",
     handler: function(resp) {
       var afterTimes = resp.data.times;
       var msg = resp.data.msg;
       if (afterTimes > times) {
         $cache.set("times", afterTimes)
         $ui.alert({
           title: "通知",
           message: msg,
           actions: [{
             title: "好的"
           }]
         })
       }
     }
   })
 }

 function main() {
   if ($cache.get("ADULT")) {
     $("checkAdult").remove()
   }
   initial();
   if ("code" in $context.query) {
     //      $ui.action($context.query)
     mode = "Search";
     $("search").text = $context.query["code"]
     keyword = $context.query["code"]
   } else {
     keyword = clipboardDetect()
     if (keyword == "none") {
       mode = "Videos";
     } else {
       mode = "Search";
       $("search").text = keyword;
     }
   }
   getVideoData();
 }

 function makeUI(title, view1, view2) {
   $ui.render({
     props: {
       title: title,
       bgcolor: $color("#dddddd"),
       id: "Avgle"
     },
     views: [view1, view2],
     layout: $layout.fill
   })
 }

 if ($app.env == $env.today) {
   var name = $addin.current.name.split(".js")
   $app.openURL("jsbox://run?name=" + name[0])
 } else {
   makeUI("Avgle", VFView, checkAdultView)
   LocalDataPath = "drive://Avgle.json";
   scriptVersionUpdate()
   alert()
   main()
 }
